<script>
import { provide, toRefs } from 'vue'
export default {
  name: 'XtxTabs',
  props: {
    modelValue: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const { modelValue } = toRefs(props)
    provide('activeName', modelValue)
    const clickFn = (item, index) => {
      console.log('点击了', index , item)
      emit('update:modelValue', item.props.name)
      emit('tab-click', { tab: item, index: index })
    }
    return {
      clickFn
    }
  },
  render () {
    console.log('tabs的slot', this.$slots.default())
    const arr = this.$slots.default()
    const panels = []
    arr.forEach(item => {
      // 如果item.type.name === 'XtxTabsPanel' 代表是写死的状况
      if (item.type.name === 'XtxTabsPanel') {
        panels.push(item)
      } else {
        if (item.children && item.children[0]?.type?.name === 'XtxTabsPanel') {
          item.children.forEach(panel => {
            panels.push(panel)
          })
        }
      }
    })
    const navs = (
      <nav>
        {
          panels.map((item, index) => {
            return (
              <a
                class={ {active: this.modelValue === item.props.name} }
                onClick= {() => this.clickFn(item, index)}
              >{item.props.label}</a>
            )
          })
        }
      </nav>
    )
    return (
      <div class="xtx-tabs">
        {[navs, panels]}
      </div>
    )
  }
}
</script>
<style lang="less">
.xtx-tabs {
  background: #fff;
  > nav {
    height: 60px;
    line-height: 60px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;
    > a {
      width: 110px;
      border-right: 1px solid #f5f5f5;
      text-align: center;
      font-size: 16px;
      &.active {
        border-top: 2px solid @xtxColor;
        height: 60px;
        background: #fff;
        line-height: 56px;
      }
    }
  }
}
</style>
